﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using TorchView.GameContent.Data;

namespace TorchView.GameContent.Skills
{
  public sealed class SkillAffixes : IDataSerializable
  {
    public const string BLOCK_NAME = "AFFIXES";

    public SkillAffixes()
    {
      Affixes = new List<string>(1);
    }

    public void LoadFromDataBlock(DataBlock block)
    {
      if (block == null)
        throw new ArgumentNullException("block");
      if (block.Name != BLOCK_NAME)
        throw new ArgumentException("Incorrect block type, only " + BLOCK_NAME + " is supported.");

      Data = block;
      Target = block.GetPropertyValue("TARGET", string.Empty);
      TargetType = block.GetPropertyValue("TARGETTYPE", string.Empty);
      AffixLevel = block.GetPropertyValue("AFFIXLEVEL", 0);
      Duration = block.GetPropertyValue("DURATION", 0f);
      AdditionalDesc = block.GetPropertyValue("ADDITIONALDESCRIPTION", string.Empty);

      Affixes.Clear();
      Affixes.AddRange(block.GetProperties("AFFIX").Select(p => p.Value as string).Where(s => s != null));
    }

    public DataBlock MakeDataBlock()
    {
      var block = Data == null ? new DataBlock(BLOCK_NAME) : Data;
      block.SetPropertyValue("TARGET", Target);
      block.SetPropertyValue("TARGETTYPE", TargetType);
      block.SetPropertyValue("AFFIXLEVEL", AffixLevel);
      block.SetPropertyValue("DURATION", Duration);
      block.SetPropertyValue("ADDITIONALDESCRIPTION", AdditionalDesc);

      block.RemoveAllProperties(p => p.Name == "AFFIX");
      foreach (var affix in Affixes)
        block.Properties.Add(new DataProperty { Name = "AFFIX", Value = affix, Type = PropertyType.STRING });

      return block;
    }

    public DataBlock Data { get; set; }

    [Category("AFFIXES"), DisplayName("Additional Desc."), Description("Additional Description for Skill")]
    public string AdditionalDesc { get; set; }

    [Category("AFFIXES"), Description("Affix List"), DisplayName("Affixes")]
    public List<string> Affixes { get; private set; }

    [DisplayName("Level"), Category("AFFIXES"), Description("Level of Affixes")]
    public int AffixLevel { get; set; }

    [Category("AFFIXES"), DisplayName("Duration"), Description("Default Duration for all Effects in Affix")]
    public float Duration { get; private set; }

    [DisplayName("Target Type"), Category("AFFIXES"), Description("Affixes Target")]
    public string Target { get; private set; }

    [DisplayName("Target Unittype"), Category("AFFIXES"), Description("Affixes target unittype")]
    public string TargetType { get; private set; }

    public override string ToString()
    {
      return BLOCK_NAME;
    }

  }
}
